﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Licensed under the MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////
#include "QtNotificationHistoryPanel.h"

#include "GbmpNew.h"
#include "QtNotificationMessageBox.h"
#include "UiNotificationManager.h"

#include "INotification.h"
#include "INotificationManager.h"

#include <QBoxLayout>
#include <QIcon>
#include <QScrollArea>
#include <QScrollBar>

#include "EnableCompileWarning_The_LAST_IncludeInCpp.h"

using namespace gcmp;


namespace
{
    const int s_maxDisplaySize = 50;
    const double s_opacity = 0.65;
    const int s_minSize = 20;
    const QString s_scrollAreaName = "NotificationHistoryScrollBar";
}

// QtNotificationHistoryPanel 类实现
gcmp::QtNotificationHistoryPanel::QtNotificationHistoryPanel(QWidget* pParent) :
QWidget(pParent, Qt::FramelessWindowHint | Qt::Tool)
{
    setWindowOpacity(s_opacity);
    setStyleSheet("QWidget{"
        "background-color: #000000;"
        "padding: 0px;"
        "margin: 0px;"
        "border-width: 0px;}");
}

void gcmp::QtNotificationHistoryPanel::BuildUi()
{
    qDeleteAll(children());

    auto area = NEW_AS_OWNER_PTR(QVBoxLayout);
    area->setContentsMargins(0, 0, 0, 0);
    auto scrollArea = NEW_AS_OWNER_PTR(QScrollArea);
    scrollArea->setObjectName(s_scrollAreaName);
    scrollArea->setMinimumSize(s_minSize, s_minSize);
    scrollArea->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
    scrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    scrollArea->setStyleSheet("QScrollBar:vertical{"
        "width: 5px;}");
    auto listWidget = NEW_AS_OWNER_PTR(QWidget, scrollArea.get());

    auto listLayout = NEW_AS_OWNER_PTR(QVBoxLayout);
    listLayout->setContentsMargins(0, 0, 0, 0);
    listLayout->setSizeConstraint(QLayout::SetMinAndMaxSize);
    size_t notificationCount = INotificationManager::Get()->GetNotificationCount();
    size_t minDisplayIndex = 0;
    if(notificationCount < s_maxDisplaySize)
        minDisplayIndex = 0;
    else
        minDisplayIndex = notificationCount - s_maxDisplaySize;
    for (size_t i = minDisplayIndex; i < notificationCount; i++)
    {
        auto notification = INotificationManager::Get()->GetNotificationByIndex(i);
        DBG_WARN_AND_CONTINUE_UNLESS(notification, L"notification为空",L"GDMPLab",L"2024-03-30");

        auto message = NEW_AS_OWNER_PTR(QtNotificationMessageBox, notification, this, false);
        listLayout->addWidget(TRANSFER_OWNERSHIP_TO_THIRD_PARTY(message));
    }

    listWidget->setLayout(TRANSFER_OWNERSHIP_TO_THIRD_PARTY(listLayout));
    listWidget->setFocusPolicy(Qt::NoFocus);
    scrollArea->setWidget(TRANSFER_OWNERSHIP_TO_THIRD_PARTY(listWidget));
    scrollArea->setFocusPolicy(Qt::NoFocus);

    area->addWidget(TRANSFER_OWNERSHIP_TO_THIRD_PARTY(scrollArea));
    setLayout(TRANSFER_OWNERSHIP_TO_THIRD_PARTY(area));
}

void gcmp::QtNotificationHistoryPanel::focusOutEvent( QFocusEvent * pEvent)
{
    QWidget::focusOutEvent(pEvent);

    UiNotificationManager::Get()->CloseNotification();
}

void gcmp::QtNotificationHistoryPanel::showEvent(QShowEvent * pEvent)
{
    QWidget::showEvent(pEvent);

    auto scrollArea = findChild<QScrollArea*>(s_scrollAreaName);
    DBG_WARN_AND_RETURN_VOID_UNLESS(scrollArea, L"scrollArea为空",L"GDMPLab",L"2024-03-30");

    auto scrollBar = scrollArea->verticalScrollBar();
    DBG_WARN_AND_RETURN_VOID_UNLESS(scrollBar, L"scrollBar为空",L"GDMPLab",L"2024-03-30");

    scrollBar->setValue(scrollBar->maximum());
}
